<?php
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];
include_once $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();
$mode=$_GET["mode"];
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Time Table Detail :.</title>
<?php
require_once($rootPath . "/" . $contextPath . "/includes/css_inc.php");
require_once($rootPath . "/" . $contextPath . "/includes/js_inc.php");
?>
<script type="text/javascript">
$(document).ready(function() {
	var events = [];
	$.ajax({
		  type: "GET",
		  url: "../actions/doctor_time_table_action.php",
		  data: {mode:"getAllData"},
		  contentType: "application/json; charset=utf-8", 
		  dataType: "json",
		  async: false,
          cache: false, 
		  success: function(data){
			for(var i=0;i<=data.length;i++){
			var mySplitDate = data[i].date.split("/");
			var startDate = new Date(mySplitDate[2],parseInt(mySplitDate[1])-1,mySplitDate[0]);
			events.push({
				id : data[i].doctor_time_table_id,
				title: data[i].start_time+" - "+data[i].end_time,
				start: startDate
			});
			}
		  },
			error: function(xhr,state,exception) { alert("exception = > " + exception); }
		});
	$('#calendar').fullCalendar({
			theme: true,
			editable: true,
			header: {
				left: 'prev,next today',
				center: 'title',
				right: ''
			},
			events: events,
			eventClick: function(calEvent, jsEvent, view){
				window.open('popup_doctor_time_table.php?mode=update&id='+calEvent.id,'popup','width=550,height=250');
			},
			dayClick: function(date, allDay, jsEvent, view) {
				var eventDate = subStrDate(date);
				window.open('popup_doctor_time_table.php?mode=insert&date='+eventDate,'popup','width=550,height=250');
			},
			eventDrop : function(event, jsEvent, view ){
				var eventDate = subStrDate(event.start);
				$.ajax({
    		    	type: "GET",
					url: "../actions/doctor_time_table_action.php",
    		    	data:{mode:"dropUpdate",id:event.id,date:eventDate},
    		    	async: false,
    		    	cache: false, 
    		    	success: function(data){
						window.location.href = "doctor_time_table.php";
					},
    		    	error: function(xhr,state,exception) { alert("exception = > " + exception); }
    		    });//end ajax
			}
		});
	$('#calendar_for_user').fullCalendar({
			theme: true,
			header: {
				left: 'prev,next today',
				center: 'title',
				right: ''
			},
			events: events
		});
	});

		function subStrDate(date){
		var strDate = new Date(date);			
		var curr_date = splitTime(strDate.getDate());
		var curr_month = splitTime(strDate.getMonth() + 1); //months are zero based
		var curr_year = strDate.getFullYear();
		var strDate = curr_date +"/"+ curr_month +"/"+ curr_year;
		return strDate;
	}
	function splitTime(stringTime){
		var strTime = new String(stringTime);
		if(strTime.length < 2){
			strTime = "0"+strTime;
		}
		return strTime;
	}
</script>
<style type='text/css'>

	body {
		margin-top: 40px;
		text-align: center;
		font-size: 14px;
		font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
		}

	#calendar {
		width: 90%;
		margin: 0 auto;
		}

</style>
</head>
<body>
<center>
<?php
if($mode=="user"){
?>
<div id='calendar_for_user'></div>
<?
}else{
?>
<div id='calendar'></div>
<?
}?>
</center>
</body>
</html>